<?php
//-------------------------------------------------------------------
//Nazov:		Insert_Product_To_Cookie
//Popis:		Vytvorenie noveho zaznamu v Cookie
function Show_Shared_Products_After_Insert_New_Product($DB_Product_ID){
	if(($DB_Product_ID!="") or ($DB_Product_ID!="0")){
		$sql_result = mysql_query("	SELECT products.ID, products.Product_Code, products.Product_Name
									FROM products, shared_products
									WHERE	(
												(shared_products.Product_ID=$DB_Product_ID) AND
												(products.ID=shared_products.Shared_Product_ID)
											)
								");
		$result = "<b><u>".SP_ZOZNAM_SUVISIACICH_PRODUKTOV.":</u></b><br />";
		while($sql_obj = mysql_fetch_object($sql_result)){
			$result.=  "
				<a 	href=\"show_product_detail.php?ID=".$sql_obj->ID."&Count=1 \" 
					target=\"rp_product_detail\"
                >[".SP_KOD.": ".$sql_obj->Product_Code."] ".$sql_obj->Product_Name."</a>
				<br />
			";
		}
	}
    return $result;
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		Insert_Product_To_Cookie
//Popis:		Vytvorenie noveho zaznamu v Cookie
function Save_Shared_Products_From_Cookie_To_DB($DB_Product_ID){
	if ((isset($_SESSION[SHARED_PRODUCTS_SESSION])) and ($DB_Product_ID!="") and ($DB_Product_ID!=0)){
	    $CookieValue = $_SESSION[SHARED_PRODUCTS_SESSION];
	    if ($CookieValue!=""){
	      	$Arr = explode("*|*",$CookieValue);
	      	$Arr_Count = count($Arr);
	      	for($i=0;$i<$Arr_Count;$i++)
		    	if(!Product_Is_Insert($Arr[$i],$DB_Product_ID)){
					mysql_query("INSERT INTO shared_products (Product_ID,Shared_Product_ID) VALUES ($DB_Product_ID,".$Arr[$i].")");
				}
	    }
	}
}
//-------------------------------------------------------------------

//-------------------------------------------------------------------
//Nazov:		Insert_Product_To_Cookie
//Popis:		Vytvorenie noveho zaznamu v Cookie
function Unset_Shared_Products_Session(){
	if (isset($_SESSION[SHARED_PRODUCTS_SESSION]))
		$_SESSION[SHARED_PRODUCTS_SESSION]="";
		
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		Insert_Product_To_Cookie
//Popis:		Vytvorenie noveho zaznamu v Cookie
function Insert_Product_To_Cookie($Cookie_Value, $ProductID){
	if(!Product_Is_Insert_In_Cookie($Cookie_Value, $ProductID)){
		if($Cookie_Value=="")
			$Cookie_Value = $ProductID;
		else
			$Cookie_Value = "$Cookie_Value*|*$ProductID";
		return Defragment_Arr($Cookie_Value);
	}
	else{
	  	print  "<script language=\"JavaScript\">
	                <!--
	                        window.alert(\"".SP_TENTO_PRODUKT_JE_UZ_VYBRANY."\");
	                //-->
	            </script>";
		return Defragment_Arr($Cookie_Value);
	}
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		Delete_Product_From_Cookie
//Popis:		Odstranenie zaznamu z Cookie
function Delete_Product_From_Cookie($CookieValue, $Product_ID){
	$Arr = explode("*|*",$CookieValue);
	$Arr_Count = count($Arr);
	
	$Delete_Index = Get_Index($CookieValue,$Product_ID);
	
	if(($Delete_Index!=-1) and ($Delete_Index<$Arr_Count)){
		$Arr[$Delete_Index] = "";		
		return Defragment_Arr(implode("*|*",$Arr));
	}
	else
		return $CookieValue;
}
//-------------------------------------------------------------------

//-------------------------------------------------------------------
//Nazov:		Generate_Select_SQL
//Popis:		Vytvorenie SQL zo zoznamu v Cookie
//-------------------------------------------------------------------
function Generate_Select_SQL_From_Cookie($Cookie_Value){
	if ($Cookie_Value!=""){
		$Arr = explode("*|*",$Cookie_Value);
		$Arr_Count = count($Arr);
			
		if($Arr_Count<1) return "";
		
		for($i=0;$i<$Arr_Count;$i++)
			if($Arr[$i]!="")
				$where .= "(ID=".$Arr[$i].") OR ";
		
		$where = substr($where,0,-4);	
		return "SELECT * FROM products WHERE ($where)";
	}
	else
		return "";
};

function Get_Index($Arr_String,$Search_String){
	$Arr = explode("*|*",$Arr_String);
  	$Arr_Count = count($Arr);
  	for($i=0;$i<$Arr_Count;$i++){
  		if($Arr[$i]==$Search_String) return $i;  		
  	}
  	return -1;
}

function Product_Is_Insert_In_Cookie($Cookie_Value, $Product_ID){
	$Arr = explode("*|*",$Cookie_Value);
  	$Arr_Count = count($Arr);
  	for($i=0;$i<$Arr_Count;$i++){
  		if($Arr[$i]==$Product_ID) return true;  		
  	}
  	return false;
}

//-------------------------------------------------------------------
//Nazov:		Defragment_Arr
//Popis:		defragmentacia pola
function Defragment_Arr($String){
	$Arr = explode("*|*",$String);
	$Arr_count = count($Arr);
	$i=0;
	while($i<($Arr_count-1)){
	  	$ii=$i;
		while(($Arr[$i]=="") and ($ii<($Arr_count-1))){
		  	//print $Arr[$i];
			for($j=$i;$j<($Arr_count-1);$j++){
				$Arr[$j]=$Arr[$j+1];
			}
			$Arr[$j]="";
			$ii++;
		}
		$i++;
	}
	//unset
	for($i=0;$i<($Arr_count);$i++)
	  	if($Arr[$i]=="")
			unset($Arr[$i]);
	return implode("*|*",$Arr);
}
//-------------------------------------------------------------------

function Product_Is_Insert($Insert_Product_ID,$ProductID){
	$sql_result=mysql_fetch_row((mysql_query("	SELECT COUNT(*) FROM shared_products 
												WHERE ((Product_ID=$ProductID) AND (Shared_Product_ID=$Insert_Product_ID))")));
	return $sql_result[0];
}

function Insert_Product($Insert_Product_ID,$ProductID){
	if(!Product_Is_Insert($Insert_Product_ID,$ProductID)){
		@$sql_result=mysql_query("INSERT INTO shared_products (Product_ID,Shared_Product_ID) VALUES ($ProductID,$Insert_Product_ID)");
		return $sql_result;
	}
	else
		print  "<script language=\"JavaScript\">
	                <!--
	                        window.alert(\"".SP_TENTO_PRODUKT_JE_UZ_VYBRANY."\");
	                //-->
	            </script>";
	return 1;
}



function Delete_Product($Product_ID){
	@$sql_result=mysql_query("DELETE FROM shared_products WHERE Shared_Product_ID=$Product_ID");
	return $sql_result;
}



function Selected_Products_List(){
	$result="";
	$sql_result = mysql_query("SELECT products.ID FROM products,random_products_sub_category
								WHERE products.SC_ID=random_products_sub_category.SC_ID");
	while($sql_obj=mysql_fetch_object($sql_result))
		$result .= $sql_obj->ID."*|*";
		
	$sql_result = mysql_query("SELECT Product_ID FROM random_products");
	while($sql_obj=mysql_fetch_object($sql_result))
		$result .= $sql_obj->Product_ID."*|*";
		
	//odstranenie poslednych znakov *|*
	$result = substr($result,0,-3);
	return $result;
}


function Selected_Products_Total_Count($ProductID){
	$sql_result=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM shared_products WHERE Product_ID=$ProductID"));
	return ($sql_result[0]);
}


function Adm_Show_Products($ABSOLUTE_SERVER_REMOTE_ADDR,$ProductID){
	print "
		<table style=\"width:100%;border:0px solid black;\">
			<caption><b>".SP_VYBRANE_PRODUKTY."</b></caption>
			<tr style=\"background-color:".TR_BACKGOUND_COLOR_2.";\">
				<td>
					<b>".SP_KOD_PRODUKTU."</b>
				</td>
				<td colspan=\"2\">
					<b>".SP_NAZOV_PRODUKTU."</b>
				</td>
			</tr>
			";
	
	$sql_result=mysql_query("	SELECT Shared_Product_ID,Product_ID,Product_Name,Product_Code FROM shared_products, products 
								WHERE ((shared_products.Product_ID=$ProductID) AND (products.ID=shared_products.Shared_Product_ID))");
	while($sql_array = mysql_fetch_object($sql_result)){
		 print"
		 	<tr style=\"background-color:".TR_BACKGOUND_COLOR_1.";\"
			 	onmouseover=\"this.style.backgroundColor='".BUTTON_ON_OVER."';\"
				onmouseout=\"this.style.backgroundColor='".TR_BACKGOUND_COLOR_1."';\" />
		 		<td>
		 			<b><font color=\"black\"><small>
						<a 
                            href=\"javascript:\"
                            onClick=\"OpenPage(".$sql_array->Shared_Product_ID.",1);\"
							target=\"rp_product_detail\"
                        >[".SP_KOD.": ".$sql_array->Product_Code."]"."</a>
					</small></font color></b>
		 		</td>
		 		<td>
		 			<a
                        href=\"javascript:\"
                        onClick=\"OpenPage(".$sql_array->Shared_Product_ID.",1);\"
                        target=\"rp_product_detail\"
                    >[".SP_KOD.": ".$sql_array->Product_Name."]"."</a>
		 		</td>
		 		<td align=\"right\">
		 			<form action=\"adm_show_selected_products.php\" target=\"selected\">
		 				<input type=\"hidden\" name=\"Delete_Product_ID\" value=\"".$sql_array->Shared_Product_ID."\" />
		 				<input type=\"hidden\" value=\"".$_REQUEST['ProductID']."\" name=\"ProductID\" />
		 				<input type=\"submit\" value=\"x\"
							style=\"width:22px;height:15px;\"
							onmouseover=\"this.style.backgroundColor='".BUTTON_ON_OVER."'; this.style.fontWeight='bolder';\"
							onmouseout=\"this.style.backgroundColor='white'; this.style.fontWeight='normal';\" />
		 			</form>
				 </td>
		 	</tr>
		 ";
	}
	print "</table>";
}


function Adm_Show_Products_From_Cookie($CookieValue, $ABSOLUTE_SERVER_REMOTE_ADDR){
  
	do{
		$sql = Generate_Select_SQL_From_Cookie($CookieValue);
		if ($sql=="") break;
	  	
		print "
			<table style=\"width:100%;border:0px solid black;\">
				<caption><b>".SP_VYBRANE_PRODUKTY."</b></caption>
				<tr style=\"background-color:".TR_BACKGOUND_COLOR_2.";\">
					<td>
						<b>".SP_KOD_PRODUKTU."</b>
					</td>
					<td colspan=\"2\">
						<b>".SP_NAZOV_PRODUKTU."</b>
					</td>
				</tr>
				";
		
		$sql_result=mysql_query($sql);
		while($sql_array = mysql_fetch_object($sql_result)){
			 print"
			 	<tr style=\"background-color:".TR_BACKGOUND_COLOR_1.";\"
				 	onmouseover=\"this.style.backgroundColor='".BUTTON_ON_OVER."';\"
					onmouseout=\"this.style.backgroundColor='".TR_BACKGOUND_COLOR_1."';\" />
			 		<td>
			 			<b><font color=\"black\"><small>
							<a 
                                href=\"javascript:\"
								target=\"rp_product_detail\"
                                onClick=\"OpenPage(".$sql_array->ID.",1);\"
                            >[".SP_KOD.": ".$sql_array->Product_Code."]"."</a>
						</small></font color></b>
			 		</td>
			 		<td>
			 			<a
                            href=\"javascript:\"
                            target=\"rp_product_detail\"
                            onClick=\"OpenPage(".$sql_array->ID.",1);\"
                        >[".SP_KOD.": ".$sql_array->Product_Name."]"."</a>
			 		</td>
			 		<td align=\"right\">
			 			<form action=\"adm_show_selected_products.php\" target=\"selected\">
			 				<input type=\"hidden\" name=\"Delete_Product_ID\" value=\"".$sql_array->ID."\" />
			 				<input type=\"hidden\" value=\"".$_REQUEST['ProductID']."\" name=\"ProductID\" />
			 				<input type=\"submit\" value=\"x\"
								style=\"width:22px;height:15px;\"
								onmouseover=\"this.style.backgroundColor='".BUTTON_ON_OVER."'; this.style.fontWeight='bolder';\"
								onmouseout=\"this.style.backgroundColor='white'; this.style.fontWeight='normal';\" />
			 			</form>
					 </td>
			 	</tr>
			 ";
		}
		print "</table>";
	}while (false);
}

//function GetProductDetail($ProductID,$Path,$PageNo,$ActualQvantumPage,$ShowType,$SearchValue,$Category_ID,$Show_Shared_Product){
//
//}

//function PrintSharedProducts($ProductID,$Path,$PageNo,$ActualQvantumPage,$ShowType,$SearchValue,$Category_ID,$Show_Shared_Product){
//  	if (Selected_Products_Total_Count($ProductID)>0)
//		print "<font style=\"font-size:10px;font-famyly:verdana;\"><b><u>S�visiace produkty:</u></b></font><br />";
//	print "<table cellspacing=\"0\" cellpadding=\"0\" style=\"width:98%; border:0px;\">";
//
//	$sql_result = mysql_query("	SELECT	Shared_Product_ID, Product_Name, Product_Code, Product_Price, Product_Descr,
//										Product_Availability, Product_UnitType
//								FROM products, shared_products
//								WHERE ((shared_products.Product_ID=$ProductID) AND
//										(products.ID=shared_products.Shared_Product_ID))"
//							);
//	while($sql_obj = mysql_fetch_object($sql_result)){
//		$image_name = $_SERVER['DOCUMENT_ROOT']."/admin/img/".$sql_obj->Shared_Product_ID."mini.jpg";
//	  	$image_address = "http://".$_SERVER['HTTP_HOST']."/admin/img/".$sql_obj->Shared_Product_ID."mini.jpg";
//	  	$image_name_big = "http://".$_SERVER['HTTP_HOST']."/admin/img/".$sql_obj->Shared_Product_ID.".jpg";
//	  	$colspan = 1;
//
//	  	print "<tr>
//		  			<td>
//						<br />
//						<table cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%;border:0px;\">
//							<tr>
//								<td style=\"	background-image:url(skin/transparent.gif);
//												background-repeat:repeat;
//												background-position:0px 0px;
//												border-bottom:0px;\"
//								>
//									<table cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%;border:0px;\">
//										<tr>";
//											if ((file_exists($image_name)) and ($Show_Shared_Product!=$sql_obj->Shared_Product_ID)){
//												//OBRAZOK:
//				        						print "
//				        							<td style=\"width:54px;\">
//				        								<form action=\"product_detail.php\" method=\"post\" target=\"products\">
//
//				        									<input type=\"hidden\" value=\"$Path\" name=\"Path\" />
//				        									<input type=\"hidden\" value=\"$PageNo\" name=\"PageNo\" />
//				        									<input type=\"hidden\" value=\"$ActualQvantumPage\" name=\"ActualQvantumPage\" />
//				        									<input type=\"hidden\" value=\"$ShowType\" name=\"ShowType\" />
//				        									<input type=\"hidden\" value=\"$SearchValue\" name=\"SearchValue\" />
//				        									<input type=\"hidden\" value=\"$Category_ID\" name=\"Category_ID\" />
//				        									<input type=\"hidden\" value=\"$sql_obj->Shared_Product_ID\" name=\"Show_Shared_Product\" />
//
//				        									<input type=\"hidden\" value=\"$ProductID\" name=\"ProductID\" />
//															<input type=\"image\" name=\"picture\" align=\"left\" src=\"".$image_address."\" value=\"submit\"
//																style=\"	margin-top:4px;
//																			margin-bottom:0px;
//																			margin-left:0pt;
//																			margin-right:0pt;
//																			width:52px;
//																			height:39px;\"/>
//														</form>
//													</td>
//												";
//												$colspan = 2;
//											}
//											if ((file_exists($image_name)) and ($Show_Shared_Product==$sql_obj->Shared_Product_ID)){
//												//OBRAZOK:
//				        						print "
//				        							<td style=\"width:54px;\">
//
//															<input type=\"image\" name=\"picture\" align=\"left\" src=\"".$image_address."\" value=\"button\"
//																style=\"	margin-top:4px;
//																			margin-bottom:0px;
//																			margin-left:0pt;
//																			margin-right:0pt;
//																			width:52px;
//																			height:39px;\"
//																onClick=\"javascript:window.open('image_show.php?image_name_big=".$image_name_big."','Image','width=680px,height=500px,location=no,directories=no,menubar=no,resizeable=yes,status=no,toolbar=no,scrollbars=yes');\"
//															/>
//													</td>";
//												$colspan = 2;
//											}
//										//LINK (nazov produktu):
//										$href = "\"product_detail.php?ProductID=$ProductID"
//																	."&amp;Path=".rawurlencode($Path)
//	        								  						."&amp;PageNo=$PageNo"
//	        														."&amp;ActualQvantumPage=$ActualQvantumPage"
//	        														."&amp;ShowType=$ShowType"
//	        														."&amp;SearchValue=$SearchValue"
//	        														."&amp;Category_ID=$Category_ID"
//	        														."&amp;Show_Shared_Product=$sql_obj->Shared_Product_ID\"";
//										print "
//										<td>
//											<b>
//												<a href=".$href." target=\"products\">".$sql_obj->Product_Name."</a>
//											</b>
//											<br />
//										";
//										if($Show_Shared_Product==$sql_obj->Shared_Product_ID){
//											print "<br />$sql_obj->Product_Descr <br />";
//										}
//										else{
//											//print "<br /><b>$sql_obj->Product_Price SKK</b><br />";
//										print "<br /><b>";
//										//****************************modul discount********************************
//										//**************************************************************************
//										//**************************************************************************
//										$discount=1;
//										if (file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/modules/discount/discount.inc'))
//										{
//											$discount=Discount($sql_obj->Shared_Product_ID);
//										}
//										//************************modul discount********************************
//										//**************************************************************************
//										//**************************************************************************
//						        		if(PAYER_VAT=="YES")
//						        		  //pri DHPH***********************************************************
//						        		  print FormatItAsCurrency($sql_obj->Product_Price*$discount).",-SKK bez DPH</b>";
//						        		  //*******************************************************************
//						        		else
//						        		  //neplatca DPH*******************************************************
//						        		  print FormatItAsCurrency($sql_obj->Product_Price*$discount).",-SKK</b>";
//						        		  //*******************************************************************
//										}
//										print "<br />";
//										print "
//									</td>
//								</tr>
//							</table>
//						</td>
//					</tr>
//					";
//					//spodny riadok umoznujuci ulozenie produktu do kosika
//					if($Show_Shared_Product==$sql_obj->Shared_Product_ID){
//						//************************modul tables********************************
//						//**************************************************************************
//						//**************************************************************************
//						if(file_exists("admin/modules/tables/new_tables.php"))
//						{
//	                     	$sql_table= "SELECT * FROM table_value WHERE ID_product=".$ProductID;
//							$sql_name=mysql_query($sql_table);
//							print "	<tr style=\"background-image:url(skin/transparent.gif);background-repeat:repeat;\">
//										<td colspan=\"$colspan\" style=\"background-color:transparent;\">
//											<table style=\"background-color:transparent;\">" ;
//							while($sql_array_table = mysql_fetch_object($sql_name))
//								{
//								  if(mysql_num_rows($sql_name))
//								  	{
//								  	  $sql_name_row= "SELECT * FROM table_descr WHERE ID=".$sql_array_table->ID_row;
//								      $sql_name_row=mysql_fetch_object(mysql_query($sql_name_row));
//								      	print "<tr style=\"background-color:transparent;\">
//										  			<td style=\"vertical-align:top;text-align:right;width:120px;background-color:transparent;\" >
//														<b><font style=\"font-size:10px;\">".$sql_name_row->name_row." :</font></b>
//													</td>
//													<td>
//														<font style=\"font-size:10px;\">".$sql_array_table->descr_value."</font>
//													</td>
//												</tr>";
//									}
//							  	}
//							print "			</table>
//										</td>
//									</tr>
//									";
//
//						}
//						//**************************************************************************
//						//**************************************************************************
//						//**************************************************************************
//							/*
//								print "
//							</table>
//						</td>";*/
//						print "<tr><td>";//<td colspan=\"$colspan\">
//							print "<form action=\"shop_basket.php\" target=\"shop_basket\" method=\"post\">";
//								print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"
//											style=\"	width:100%;
//														background-color:".TR_BACKGOUND_COLOR_2.";
//														background-image:url(skin/product_bar.jpg);
//														background-repeat:repeat;
//														background-position:0px 0px;\"
//										><tr>";
//
//							        print "<td>cena:<b>";
//							        //************************modul discount********************************
//										//**************************************************************************
//										//**************************************************************************
//												$discount=1;
//											     if (file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/modules/discount/discount.inc'))
//													{
//												   		//require_once ($_SERVER['DOCUMENT_ROOT'].'/admin/modules/discount/discount.inc');
//											        		//print "xxx".Discount($ProductID)."xxx";
//											        		$discount=Discount($sql_obj->Shared_Product_ID);
//
//											     	}
//										//************************modul discount********************************
//										//**************************************************************************
//										//**************************************************************************
//							        if(PAYER_VAT=="YES")
//							          //pri DHPH***********************************************************
//							          print FormatItAsCurrency($sql_obj->Product_Price*$discount).",-SKK bez DPH</b>";
//							          //*******************************************************************
//							        else
//							          //neplatca DPH*******************************************************
//							          print FormatItAsCurrency($sql_obj->Product_Price*$discount).",-SKK</b>";
//							          //*******************************************************************
//
//							        switch($sql_obj->Product_Availability){
//							           case 0:print   "<font style=\"font-color:black; font-size:10px;\"> ...[nie je na sklade ]</font>";break;
//							           case 1:print   "<font style=\"font-color:black; font-size:10px;\"> ...[Je na sklade ]</font>";break; }
//							        print "</td>";
//
//        							if($discount!=1){
//									print "<td style=\"vertical-align:left;\"><font style=\"font-color:red; font-size:10px;\"><b>";
//      								  if(PAYER_VAT=="YES")
//        							  //pri DHPH***********************************************************
//         								 print FormatItAsCurrency($sql_obj->Product_Price).",-SKK bez DPH</b></font>";
//        							  //*******************************************************************
//        							  else
//        							  //neplatca DPH*******************************************************
//        								 print FormatItAsCurrency($sql_obj->Product_Price).",-SKK</b></font>";
//        							  //*******************************************************************
//        								 print "</td>";
//       								  }
//
//							        print "<td style=\"vertical-align:left;width:100px;\">";
//							          print "mno�stvo: ";
//							        print "<input type=\"text\" name=\"Count_product\" value=\"1\" size=\"3\" style=\"height:14px; font-size: 10px; font-family:verdana; border: 0px;\"/>";
//							        print "</td>";
//							        print "<td style=\"vertical-align:left;text-align:left;width:30px;\">";
//							        	print $sql_obj->Product_UnitType;
//									print "</td>";
//							        print "<td style=\"vertical-align:left; width:15px;\">";
//							        	print "<input type=\"hidden\" name=\"ID_product\" value=\"".$sql_obj->Shared_Product_ID."\">";
//							            print "<input type=\"image\" src=\"shop_basket_mini1.bmp\" value=\"submit\" alt=\"VLO�I� DO KO��KA\"
//							                        style=\"border: 0px;\"
//							                    	onmouseover=\"this.src = 'shop_basket_mini2.bmp';\"
//							                        onmouseout=\"this.src = 'shop_basket_mini1.bmp';\" />";
//							        print "</td>";
//						        print "</tr></table>";
//					        print "</form>";
//					    print "</td>";
//							}
//						print "
//				</table>
//			";
//		print "</td></tr>";
//	}
//	print "</table>";
//	print "<br />";
//}

?>